﻿#define _CRT_SECURE_NO_WARNINGS 1
// 原题连接：https://www.luogu.com.cn/problem/P3385
// 开始解题:
// 方法——bellman-ford-spfa
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 2e3 + 10;
const int M = 3e3 + 10;
int n = 0, m = 0;
int t = 0;
const int INF = 0x3f3f3f3f;
bool vis[N];
int dist[N];
int cnt[N];
int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		vector<vector<vector<int>>> edges(n + 1);
		queue<int> q;
		memset(vis, false, sizeof vis);
		memset(dist, INF, sizeof dist);
		memset(cnt, 0, sizeof cnt);
		int a = 0, b = 0, val = 0;
		for (int i = 0; i < m; i++) {
			scanf("%d%d%d", &a, &b, &val);
			if (val >= 0) {
				edges[a].push_back({ b, val });
				edges[b].push_back({ a, val });
			}
			else {
				edges[a].push_back({ b, val });
			}
		}
		vis[1] = true;
		q.push(1);
		dist[1] = 0;
		auto spfa = [&]() {
			while (!q.empty()) {
				int cur = q.front();
				q.pop();
				vis[cur] = false;
				for (auto& e : edges[cur]) {
					a = cur;
					b = e[0];
					val = e[1];
					if (dist[b] > dist[a] + val) {
						dist[b] = dist[a] + val;
						if (!vis[b]) {
							if (cnt[b]++ == n) {
								return true; // 有负环
							}
							vis[b] = true;
							q.push(b);
						}
					}
				}
			}
			return false;
		};

		if (spfa()) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}

	}
	return 0;
}